import Vue from 'vue'
import VueRouter from 'vue-router'
const Login = () =>import('../views/login/Login');
const Home = () => import('../views/home/Home');
const Welcome = () => import('../views/home/childComp/Welcome');
const Users = () => import('../views/users/Users');
const Rights = () => import('../views/power/Rights');
const Roles = () => import('../views/power/Roles');
const ShopCategory = () => import('../views/goods/ShopCategory');
const CategoryParams = () => import('../views/goods/CategoryParams');
const GoodsList = () => import('../views/goods/GoodsList');
const AddGood = () => import('../views/goods/AddGood');
const Order = () => import('../views/orders/Order');
const Report = () => import('../views/report/Report');
Vue.use(VueRouter);

  const routes = [
    {path:'/', redirect:"/login"},
    {path: '/login', component:Login},
    {path:'/home', component:Home, redirect:'/welcome',
      children:[
        {path:'/welcome', component:Welcome},
        {path: '/users', component:Users},
        {path:'/rights', component:Rights},
        {path:'/roles',component:Roles},
        {path:'/categories',component:ShopCategory},
        {path:'/params',component:CategoryParams},
        {path:'/goods',component:GoodsList},
        {path:'/add',component:AddGood},
        {path:'/orders',component:Order},
        {path:'/reports',component:Report},
      ]
    }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
});
//挂载路由导航守卫
router.beforeEach((to,from,next) => {
  if (to.path === '/login') return next();
  const tokenStr = sessionStorage.getItem('token');
  if(!tokenStr) return next('/login');
  next()
});
export default router
